package com.chat.repository;

import com.chat.model.Message;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MessageRepository extends JpaRepository<Message, Long> {
    
    @Query("SELECT m FROM Message m WHERE (m.senderId = :userId AND m.receiverId = :otherUserId) " +
           "OR (m.senderId = :otherUserId AND m.receiverId = :userId) ORDER BY m.createdAt ASC")
    List<Message> findChatMessages(@Param("userId") String userId, @Param("otherUserId") String otherUserId);
    
    List<Message> findByReceiverIdOrderByCreatedAtDesc(String receiverId);
}

